package com.gw.yunke.web;

import com.gw.yunke.dao.PartMapper;
import com.gw.yunke.dao.PartVideoMapper;
import com.gw.yunke.dao.SectionMapper;
import com.gw.yunke.domain.Part;
import com.gw.yunke.domain.PartVideo;
import com.gw.yunke.domain.Section;
import com.gw.yunke.domain.User;
import com.gw.yunke.domain.Video;
import com.gw.yunke.domain.dto.ResultData;
import com.gw.yunke.service.IUserService;
import com.gw.yunke.service.impl.MultipartFileService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 用于处理图片视频上传请求
 */

@RestController
public class UploadController {

  private static final Logger log = LoggerFactory.getLogger(UploadController.class);

  @Autowired
  private MultipartFileService multipartFileService;
  @Autowired
  private SectionMapper sectionMapper;
  @Autowired
  private PartMapper partMapper;
  @Autowired
  private IUserService userService;
  @Autowired
  private PartVideoMapper partVideoMapper;

  @RequestMapping(value = "/uploadVideo", method = RequestMethod.POST)
  public ResultData upLoadVideo(@RequestParam(value = "file", required = true) MultipartFile multipartFile, @CookieValue("sso") String token) throws Exception {
    log.info("接收上传视频");
//        String path = "E:/";
//        File videoFile = new File(path,file.getOriginalFilename());
//
//        file.transferTo(videoFile);
    User user = new User();
    userService.checkLogin(token, user);
    if(!user.getType()){
      throw new Exception("没有教师权限");
    }
    ResultData resultData = new ResultData();
    Long videoId = -1L;
    //Section section = sectionMapper.selectByPrimaryKey(part.getSectionId());
    if (multipartFile != null)
      videoId = multipartFileService.upLoadVideo(multipartFile, user);

    resultData.setCode(ResultData.OK);
    if (videoId > 0)
      resultData.setData(videoId);
    else
      resultData.setData(-1);

    return resultData;
  }

  public String upLoadImage(@RequestParam(value = "image", required = true) MultipartFile multipartFile, HttpServletRequest request) {

    return "";
  }


  @RequestMapping(value = "/upLoadPPT",method = RequestMethod.POST)
  public ResultData upLoadPPT(@RequestParam(value = "file", required = true) MultipartFile multipartFile, @RequestParam("courseId")Long courseId,@CookieValue("sso") String token) throws Exception {
    log.info("接收上传视频");
//        String path = "E:/";
//        File videoFile = new File(path,file.getOriginalFilename());
//
//        file.transferTo(videoFile);
    User user = new User();
    userService.checkLogin(token, user);
    if(!user.getType()){
      throw new Exception("没有教师权限");
    }
    ResultData resultData = new ResultData();
    int result = 0;
    //Section section = sectionMapper.selectByPrimaryKey(part.getSectionId());
    if (multipartFile != null)
      result = multipartFileService.upLoadPPT(multipartFile,courseId,user);

    resultData.setCode(ResultData.OK);
    resultData.setData(result);

    return resultData;
  }

  @RequestMapping(value = "/batchUpload", method = RequestMethod.POST)
  public String batchUploadVideo(List<MultipartFile> multipartFileList) {
    String path = "E:/";
    try {
      for (MultipartFile file : multipartFileList) {
        File videoFile = new File(path, file.getOriginalFilename());

        file.transferTo(videoFile);
      }
    } catch (IOException e) {
      log.error("批量上传发生错误", e);
    }
    return "ok";
  }




  @RequestMapping(value = "/image/*")
  public String get() {
    return "11049134.jpg";
  }
}
